﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireBias.Models;
using JQueryDataTables.Models;

namespace TireBias.Controllers
{
    public class MasterController : Controller
    {
        //
        // GET: /Master/

        public ActionResult Index()
        {
            WasteModel rs = new WasteModel();
            List<WasteModel.Waste_DS> list = rs.SEL_WASTE_MASTER("");
            return View(list);
        }
        [Authorize]
        public ActionResult LIST_WASTE()
        {
       
            return View();
        }

        public JsonResult GetLIST_WASTE()
        {
            int page = int.Parse(Request.Form["page"]);
            int rp = int.Parse(Request.Form["rp"]);
            string query = Request.Form["query"].ToString();
            string qtype = Request.Form["qtype"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            List<WasteModel.Waste_DS> dt;
            WasteModel rs = new WasteModel();
            if (qtype == "_des")
            {
                     dt  = rs.SEL_WASTE_MASTER("",query);
            }else   dt = rs.SEL_WASTE_MASTER(query);

            int count = dt.Count;
            dt = dt.Skip((page - 1) * rp).Take(rp).ToList();
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            return Json(new
            {
                page = Request["page"],
                total = count,
                rows = dt
            });
        }

        public JsonResult INS_WASTE_DATA(TireBias.Models.WasteModel.Waste_DS theData,string Mode)
        {
            try
            {

                TireBias.Models.WasteModel mdUpd = new TireBias.Models.WasteModel();
                theData._ModifyBy = User.Identity.Name.ToString();


                if (Mode == "A")
                {
                    List<WasteModel.Waste_DS> dt = mdUpd.SEL_WASTE_MASTER(theData._code);
                    if (dt.Count > 0)
                    {
                        return Json(new { success = 0, message = "ไม่สามารถบันทึกได้ ระบุ Code ซ้ำ" });
                    }

                    if (mdUpd.Ins_Master(theData))
                    {
                        return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
                    }
                }
                if (Mode == "E")
                {
                    if (mdUpd.UPD_Master(theData))
                    {
                        return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
                    }
                }

               
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
               
            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                throw;
            }



        }

        [Authorize]
        public ActionResult LIST_LOSS(){
            
            return View();

        }

        public JsonResult GetLIST_LOSS()
        {
            int page = int.Parse(Request.Form["page"]);
            int rp = int.Parse(Request.Form["rp"]);
            string query = Request.Form["query"].ToString();
            string qtype = Request.Form["qtype"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            List<LossModel.LOSS_DS> dt;
            LossModel rs = new LossModel();
            if (qtype == "_des")
            {
                dt = rs.SEL_LOSS_MASTER("", query);
            }
            else dt = rs.SEL_LOSS_MASTER(query);

            int count = dt.Count;
            dt = dt.Skip((page - 1) * rp).Take(rp).ToList();
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            return Json(new
            {
                page = Request["page"],
                total = count,
                rows = dt
            });
        }

        public JsonResult INS_LOSS_DATA(TireBias.Models.LossModel.LOSS_DS theData, string Mode)
        {
            try
            {

                TireBias.Models.LossModel mdUpd = new TireBias.Models.LossModel();
                theData._ModifyBy = User.Identity.Name.ToString();


                if (Mode == "A")
                {
                    List<LossModel.LOSS_DS> dt = mdUpd.SEL_LOSS_MASTER(theData._code);
                    if (dt.Count > 0)
                    {
                        return Json(new { success = 0, message = "ไม่สามารถบันทึกได้ ระบุ Code ซ้ำ" });
                    }

                    if (mdUpd.Ins_Master(theData))
                    {
                        return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
                    }
                }
                if (Mode == "E")
                {
                    if (mdUpd.UPD_Master(theData))
                    {
                        return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
                    }
                }


                return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });

            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                throw;
            }



        }


    }
}
